<template>
  <div class="app-container">
    <el-button type="success" icon="el-icon-finished" style="margin: 10px;" @click="handleSubmit">
      提交
    </el-button>
    <el-button type="info" icon="el-icon-refresh" style="margin: 10px;" @click="handleRefresh">
      重置
    </el-button>
    <el-button type="info" icon="el-icon-circle-close" style="margin: 10px;" @click="handleClose">
      关闭
    </el-button>
    <el-button type="danger" icon="el-icon-delete" style="margin: 10px;" @click="handleDelete">
      删除
    </el-button>

    <el-form ref="channelDeliveryChargeEditForm" :rules="channelDeliveryChargeRules" :model="channelDeliveryCharge" label-width="80px" style="margin-left:0px;">
      <el-form-item label="渠道" prop="channelId">
        <el-select v-model="channelDeliveryCharge.channelId" clearable filterable>
          <el-option v-for="item in channelOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="省份" prop="province">
        <el-select v-model="channelDeliveryCharge.province" clearable filterable>
          <el-option v-for="item in provinceOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="快递公司" prop="deliveryTypeId">
        <el-select v-model="channelDeliveryCharge.deliveryTypeId" clearable filterable>
          <el-option v-for="item in deliveryTypeOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="首件邮费" prop="firstPrice">
        <el-input v-model="channelDeliveryCharge.firstPrice" type="number" step="0.1" :min="0" :max="1000" style="width: 100px;" />
      </el-form-item>
      <el-form-item label="续件邮费" prop="addPrice">
        <el-input v-model="channelDeliveryCharge.addPrice" type="number" step="0.1" :min="0" :max="1000" style="width: 100px;" />
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { fetchEnum } from '@/api/system-api'
import { getSupplierDeliveryChargeById, putSupplierDeliveryChargeById, deleteSupplierDeliveryChargeById } from '@/api/manager/channel-api'
import { toOptionsList } from '@/utils/index'

export default {
  name: 'ManagerChannelSupplierDeliveryChargeEdit',
  filters: {
  },
  data() {
    return {
      channelOptions: [],
      provinceOptions: [],
      deliveryTypeOptions: [],
      channelDeliveryCharge: {
        id: null,
        channelId: null,
        province: null,
        deliveryTypeId: null,
        firstPrice: null,
        addPrice: null
      },
      channelDeliveryChargeRules: {
        channelId: [{
          required: true, message: '请输入渠道', trigger: 'blur'
        }],
        province: [{
          required: true, message: '请输入省份', trigger: 'blur'
        }],
        deliveryTypeId: [{
          required: true, message: '请输入快递公司', trigger: 'blur'
        }],
        firstPrice: [{
          required: true, message: '请输入首件邮费', trigger: 'blur'
        }],
        addPrice: [{
          required: true, message: '请输入续件邮费', trigger: 'blur'
        }]
      }
    }
  },
  async created() {
    await this.initLoad()
    await this.loadDetail()
  },
  methods: {
    async initLoad() {
      const enumCodes = ['SUPPLIER_CHANNEL', 'PROVINCE', 'DELIVERY_TYPE']
      const { data } = await fetchEnum(enumCodes)
      for (const e of data) {
        if (e.code === 'SUPPLIER_CHANNEL') {
          if (e.options) {
            this.channelOptions = toOptionsList(e.options, 'id', 'shortName')
          }
        } else if (e.code === 'PROVINCE') {
          if (e.options) {
            this.provinceOptions = toOptionsList(e.options, 'code', 'name')
          }
        } else if (e.code === 'DELIVERY_TYPE') {
          if (e.options) {
            this.deliveryTypeOptions = toOptionsList(e.options, 'id', 'name')
          }
        }
      }
    },
    loadDetail() {
      getSupplierDeliveryChargeById(this.$route.params.id).then(response => {
        const { data } = response
        this.channelDeliveryCharge = data
      })
    },

    handleSubmit() {
      this.$refs.channelDeliveryChargeEditForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.channelDeliveryCharge))
        putSupplierDeliveryChargeById(this.channelDeliveryCharge.id, data).then(response => {
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 1 * 1000
          })
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    },
    handleRefresh() {
      this.loadDetail()
    },
    handleClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    },
    handleDelete() {
      deleteSupplierDeliveryChargeById(this.channelDeliveryCharge.id).then(response => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 1 * 1000
        })
        this.handleClose()
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
      })
    }

  }
}

</script>
